package com.qifan.energy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.vo.UpdateInfoVO;
import com.qifan.energy.R;
import com.qifan.energy.bean.result.MemberInfo;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.EmojiUtils;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.dialog.LoadingUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdateNicknameActivity extends BaseActivity {

    @BindView(R.id.nickname_et)
    EditText nicknameEt;

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_username);
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_username;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();
    }

    @Override
    protected void initData() {
        /* 获取本地用户信息 */
        MemberInfo result = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
        String nickname = result.getNickname();
        if (!TextUtils.isEmpty(nickname)){
            nicknameEt.setText(nickname);
            nicknameEt.setSelection(nicknameEt.getText().length());
        }
    }

    /* 更改用户信息 */
    @OnClick(R.id.done_btn)
    void onDoneBtnClicked(TextView btn){
        String nickname = nicknameEt.getText().toString();
        if (TextUtils.isEmpty(nickname.trim())){
            ToastUtils.showShort("昵称输入不能为空哦~");
            return;
        }
        if (EmojiUtils.containsEmoji(nickname)){
            ToastUtils.showShort("昵称不能含表情哦~");
            return;
        }
        btn.setEnabled(false);
        LoadingUtils.show(this);
        UpdateInfoVO updateInfoVO = new UpdateInfoVO();
        updateInfoVO.setNickname(nickname);
        NetUtils.updateInfo(updateInfoVO, new MyObserver<MemberInfo>(this) {

            @Override
            public void onSuccess(MemberInfo result) {
                //更新用户信息保存本地
                SharedPreferencesUtil.putData(Keys.USER_INFO_MEMBER_KEY,result);
                //指示器
                btn.setEnabled(true);
                LoadingUtils.hide();
                //提示
                ToastUtils.showShort("修改成功!");
                //反向传值
                Intent intent = new Intent();
                intent.putExtra(Keys.NICKNAME_KEY,nickname);
                setResult(RESULT_OK, intent);
                //跳转
                ActivityStackUtil.finishTopActivity();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                btn.setEnabled(true);
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);
            }
        });
    }
}
